package views;

import java.util.Hashtable;
import java.util.List;

import persistence.DataBaseController;
import utils.ShowException;

import com.example.android.google.apis.R;
import com.example.android.google.apis.com.example.android.apis.MapsDemo;

import docks.Dock;
import docks.DockController;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class DockStarred extends ListActivity {

    public void onCreate(Bundle icicle) {

	super.onCreate(icicle);
	setContentView(R.layout.list);
	Dock[] docks = getItems();
	setListAdapter(new IconicAdapter(docks));
	ListController.setDockStarred(this);

	getListView().setOnItemClickListener(new OnItemClickListener() {

	    @Override
	    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
		    long arg3) {
		
		Dock d = (Dock) arg0.getItemAtPosition(arg2);
		Intent intent = new Intent(DockStarred.this, MapsDemo.class);
		intent.putExtra("moveToDock", true);
		intent.putExtra("latitude", d.getLatitude());
		intent.putExtra("longitude", d.getLongitude());
		startActivity(intent);
	    }
	    
	    
	});
    }

    @Override
    public void onDestroy() {

	super.onDestroy();
	DataBaseController.getInstance().closeDatabase();
    }

    public void setListAdapter() {

	setListAdapter(new IconicAdapter(getItems()));
    }

    protected Dock[] getItems() {

	List<Dock> docks = DockController.getInstance(this).getStarredDocks();

	Dock[] ad = new Dock[docks.size()];
	
	TextView tv = (TextView) findViewById(R.id.selection);
	if (ad.length == 0) {
	    
	    tv.setText("This list is currently empty. The docks you star will appear in this list.");
	} else {
	    
	   tv.setText(null);
	}
	
	return docks.toArray(ad);
    }

    class IconicAdapter extends ListController {

	// TODO use the whole list making the unstarred elements unvisible!

//	Dock[] items;

	IconicAdapter(Dock[] docks) {

	    super(DockStarred.this, R.layout.row, docks);
//	    this.items = docks;
	    ListController.setStarredAdapter(this);
	}

	public void reloadDocks(Dock[] docks) {

	    this.items = docks;
	    ListController.setStarredAdapter(this);
	}

	public View getView(final int position, View convertView,
		ViewGroup parent) {
	    
	    if (!items[position].isStarred())
		return null;

	    LayoutInflater inflater = getLayoutInflater();
	    View row = inflater.inflate(R.layout.row, parent, false);
	    TextView label = (TextView) row.findViewById(R.id.labelname);
	    TextView free = (TextView) row.findViewById(R.id.labelfree);
	    TextView available = (TextView) row
		    .findViewById(R.id.labelavailable);
	    final ImageView ib = (ImageView) row
		    .findViewById(R.id.imageButton1);

	    if (items[position].isStarred()) {

		ib.setImageDrawable(getResources().getDrawable(
			R.drawable.starred));
	    } else {

		ib.setImageDrawable(getResources().getDrawable(
			R.drawable.no_starred));
	    }

	    ib.setOnClickListener(new OnClickListener() {

		@Override
		public void onClick(View v) {

		    DockController.getInstance().changeStarred(
			    items[position].getIdDock());
		    // notifyDataSetChanged();
		    ListController.notifyChanges();
		    ListController.notifyChangesToAll();
		}
	    });

	    label.setText(items[position].getIdDock() + " - "
		    + items[position].getAddress());
	    free.setText("Free slots: " + Integer.toString(items[position].getFreeSlots()));
	    available.setText("Available bikes: " + Integer.toString(items[position].getAvailableBikes()));

	    return (row);
	}
    }
}
